import {
  userDetail,
  userLogin,
  updateUserInfo,
  chiefProfit,
  myPoster,
  withdrawApply
} from '../../utils/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    show: false,
    email: '',
    chiefProfit: {},
    identity: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      identity: wx.getStorageSync('identity')
    })
    this.validateFormal().then((res) => {
      if (res) {
        this.userDetail()
        this.chiefProfit()
      }
    })
  },
  /**
   * 分享给好友
   * @param {*} options 
   */
  onShareAppMessage: function (options) {
    return {
      title: '外卖、团购、优惠省钱',
      imageUrl: 'https://note.youdao.com/yws/public/resource/ad9e6971e9586afefaeec5a338765182/xmlnote/16BF8A9EB2C04BF5A976F11499DC5639/3599',
      path: '/pages/index/index'
    }
  },
  /**
   * 获取用户信息
   */
  userDetail() {
    userDetail().then(res => {
      this.setData({
        userInfo: res.data
      })
      wx.setStorageSync('formal', res.data.formal)
    })
  },
  /**
   * 验证用户是否为正式用户
   */
  validateFormal() {
    return new Promise((resolve, reject) => {
      if (wx.getStorageSync('formal') === 0) {
        wx.showToast({
          title: '请先登录',
          icon: 'error'
        })
      } else {
        resolve(true)
      }
    })
  },
  /**
   * 获取小程序用户信息
   */
  getUserProfile() {
    var that = this
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: res => {
        that.userLogin(res.userInfo)
      }
    })
  },
  /**
   * 用户登录 等同于修改用户信息
   */
  userLogin(e) {
    userLogin({
      avatarUrl: e.avatarUrl,
      nickName: e.nickName
    }).then(res => {
      if (res.data > 0) {
        wx.showToast({
          title: '登录成功',
          icon: 'none'
        })
        this.userDetail()
      }
    })
  },
  /**
   * 显示page-container
   */
  showPageContainer() {
    this.validateFormal().then(() => {
      this.setData({
        show: true
      })
    })
  },
  /**
   * 修改用户信息
   */
  updateUserInfo() {
    if (!this.data.userInfo.email) {
      wx.showToast({
        title: '邮箱不能为空',
        icon: 'error'
      })
      return false
    }
    updateUserInfo({
      email: this.data.userInfo.email
    }).then(res => {
      if (res.data > 0) {
        wx.showToast({
          title: '成功',
          icon: 'none'
        })
        this.setData({
          show: false
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  /**
   * 获取用户佣金信息
   */
  chiefProfit() {
    chiefProfit().then(res => {
      this.setData({
        chiefProfit: res.data
      })
    })
  },
  /**
   * 输入邮箱
   */
  onInputEmail(e) {
    this.setData({
      'userInfo.email': e.detail.value
    })
  },
  /**
   * 修改支付宝信息
   */
  updateZFB() {
    var that = this
    this.validateFormal().then(() => {
      wx.navigateTo({
        url: '../zfb/zfb',
        success: function (res) {
          res.eventChannel.emit('updateZfb', {
            data: {
              zfb: that.data.userInfo.zfb,
              zfbName: that.data.userInfo.zfbName
            }
          })
        }
      })
    })
  },
  /**
   * 生成海报
   */
  createPoster() {
    myPoster().then(res => {
      wx.navigateTo({
        url: '../poster/poster?url=' + res.data,
      })
    })
  },
  /**
   * 联系开发者
   */
  touchDeveloper() {
    wx.previewImage({
      urls: ['https://chage.club/cache/common/developer.jpg']
    })
  },
  /**
   * 申请提现
   */
  withdrawApply() {
    var that = this
    this.validateFormal().then(() => {
      wx.showModal({
        title: '提现',
        content: '提现支付宝:' + this.data.userInfo.zfb,
        success(res) {
          if (res.confirm) {
            const templateId = 'FPuY5y0eygYPY_0NAqqs0usZ9nGpYniWVmk2gOB7oLs'
            wx.requestSubscribeMessage({
              tmplIds: [templateId],
              success: res => {
                if (res[templateId] === 'accept') {
                  withdrawApply().then(res => {
                    if (res.data) {
                      wx.showToast({
                        title: '申请成功',
                        icon: 'success'
                      })
                      that.chiefProfit()
                    } else {
                      wx.showToast({
                        title: '申请失败',
                        icon: 'error'
                      })
                    }
                  })
                }
              },
              fail: res => {
                console.log('fail', res)
              }
            })
          } else if (res.cancel) {
            wx.showToast({
              title: '您已取消',
              icon: 'none'
            })
          }
        }
      })
    })
  },
  /**
   * 提现
   */
  withdraw() {
    wx.navigateTo({
      url: '../withdraw/withdraw'
    })
  },
  /**
   * 我的订单
   */
  myOrder() {
    wx.navigateTo({
      url: '../order/order'
    })
  },
  /**
   * 团队
   */
  team() {
    wx.navigateTo({
      url: '../team/team'
    })
  },
  /**
   * 新手必读
   */
  readDetail() {
    wx.navigateTo({
      url: '../readDetail/readDetail',
    })
  }
})